package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import entity.Battle;
import entity.User;
import exception.DAOException;
import gobang_util.DbUtil;

public class BattleDataDAO {

	// 数据库数据
	public void Insert(Battle battle) {
		String sql = "insert into battle_data(black_account,white_account,battle_time,result) values(?,?,now(),?)";
		DbUtil.executeSQL(sql, battle.getBlackAccount(), battle.getWhiteAccount(), battle.getResult());
	}

	public void delete(int id) {
		String sql = "delete from battle_data where id=?";
		DbUtil.executeSQL(sql, id);
	}

	public void deleteUser(int id) {
		String sql = "delete from user_data where id=?";
		DbUtil.executeSQL(sql, id);
	}

	public void update(Battle battle, int id) {
		String sql = "update battle_data set id=?,black_account=?,white_account=?,battle_time=?,result=? where id=?";
		DbUtil.executeSQL(sql, battle.getId(), battle.getBlackAccount(), battle.getWhiteAccount(),
				battle.getBattleTime(), battle.getResult(), id);
	}

	public Battle select(int id, int connectFlag) {
		Battle bt = new Battle();
		String sql = "select * from battle_data where id = ?";
		Connection connect = DbUtil.getConnection();
		try {
			PreparedStatement pstmt = connect.prepareStatement(sql);
			pstmt.setInt(1, id);
			ResultSet rs = pstmt.executeQuery();

			if (rs.next()) {
				bt.setId(rs.getInt("id"));
				bt.setBlackAccount(rs.getString("black_account"));
				bt.setWhiteAccount(rs.getString("white_account"));
				bt.setBattleTime(rs.getString("battle_time"));
				bt.setResult(rs.getInt("result"));
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return bt;
	}

	public List<Battle> selectAll() {
		List<Battle> ls = new ArrayList<>();
		String sql = "select * from battle_data";
		Connection connect = DbUtil.getConnection();
		try {
			PreparedStatement pstmt = connect.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();

			while (rs.next()) {
				Battle bt = new Battle();
				bt.setId(rs.getInt("id"));
				bt.setBlackAccount(rs.getString("black_account"));
				bt.setWhiteAccount(rs.getString("white_account"));
				bt.setBattleTime(rs.getString("battle_time"));
				bt.setResult(rs.getInt("result"));
				ls.add(bt);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ls;
	}

	/**
	 * 根据用户获取对战记录
	 * 
	 * @return
	 */
	public List<Battle> getRecordByAccount(String account) {
		List<Battle> ls = new ArrayList<>();
		String sql = "select * from battle_data where black_account = ? or white_account = ?";
		Connection connect = DbUtil.getConnection();
		try {
			PreparedStatement pstmt = connect.prepareStatement(sql);
			pstmt.setString(1, account);
			pstmt.setString(2, account);
			ResultSet rs = pstmt.executeQuery();

			while (rs.next()) {
				Battle bt = new Battle();
				bt.setId(rs.getInt("id"));
				bt.setBlackAccount(rs.getString("black_account"));
				bt.setWhiteAccount(rs.getString("white_account"));
				bt.setBattleTime(rs.getString("battle_time"));
				bt.setResult(rs.getInt("result"));
				ls.add(bt);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new DAOException();
		}
		return ls;

	}
}
